﻿using DevExpress.XtraTab;
using DevExpress.XtraTab.ViewInfo;
using System;
using System.Windows.Forms;

namespace HenrryTools.WinFormTools
{
    public partial class MenusHome : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public MenusHome()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 控件
        /// </summary>
        private XtraTabControl xtraTabControHome = new XtraTabControl();

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFormat_Click(object sender, EventArgs e)
        {
            xtraTabControHome.Name = "xtraTabControlFormatWinform";
            FormatWinForm formatWinForm = new FormatWinForm();
            LoadTabPageForm(xtraTabControl, formatWinForm, "序列化");
        }
        /// <summary>
        /// 打开SM4国标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSm4_Click(object sender, EventArgs e)
        {
            xtraTabControHome.Name = "Sm4WinForm";
            Sm4WinForm Sm4WinForm = new Sm4WinForm();
            LoadTabPageForm(xtraTabControl, Sm4WinForm, "SM4国标");
        }
        /// <summary>
        /// 关闭table
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xtraTabControl_CloseButtonClick(object sender, EventArgs e)
        {
            XtraTabControl tabControl = sender as XtraTabControl;
            ClosePageButtonEventArgs arg = e as ClosePageButtonEventArgs;
            var page = arg.Page as XtraTabPage;
            if (page.HasChildren)
            {
                tabControl.TabPages.Remove(page);
                page.Controls.Clear();
                page.Dispose();
            }


        }
        /// <summary>
        /// 将控件添加到标签页中
        /// </summary>
        /// <param name="PageName"></param>
        /// <param name="ctrl"></param>
        private void LoadTabPageForm(XtraTabControl tabcontrol, Form form, String title)
        {
            bool found = false;
            XtraTabPage selectedPage = null;
            //是否允许多开页面
            //foreach (XtraTabPage page in tabcontrol.TabPages)
            //{
            //    if (page.Tag != null && page.Text == title)
            //    {
            //        found = true;
            //        selectedPage = page;
            //        break;
            //    }
            //}
            if (!found)
            {
                selectedPage = new XtraTabPage();
                selectedPage.Text = title;
                selectedPage.Tag = form;
                form.TopLevel = false;
                form.Dock = DockStyle.Fill;
                form.FormBorderStyle = FormBorderStyle.None;
                form.Show();
                selectedPage.Controls.Clear();
                selectedPage.Controls.Add(form);
                form.Visible = true;
                if (!form.IsDisposed)
                {
                    tabcontrol.SelectedTabPage = selectedPage;
                    tabcontrol.TabPages.Add(selectedPage);   //把tabpage加入到tabcontrol里
                }
            }
            else
            {
                if (selectedPage.Tag != null && selectedPage.Tag != form)
                {
                    form.TopLevel = false;
                    form.Dock = DockStyle.Fill;
                    form.FormBorderStyle = FormBorderStyle.None;
                    form.Show();
                    selectedPage.Controls.Clear();
                    selectedPage.Controls.Add(form);
                }
            }
            selectedPage.BringToFront();
            tabcontrol.SelectedTabPage = selectedPage;
        }
        /// <summary>
        /// 默认加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenusHome_Load(object sender, EventArgs e)
        {
            ribbonControl1.Minimized = true;//默认收缩
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xtraTabControHome.Name = "xtraTabControlFormatWinform";
            FormatWinForm formatWinForm = new FormatWinForm();
            LoadTabPageForm(xtraTabControl, formatWinForm, "序列化");
        }

        private void MenusHome_Shown(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;//窗体默认最大化
        }
        /// <summary>
        /// 清空缓存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem9_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Properties.Settings.Default.Reset();
            MessageBox.Show("清空缓存成功！");
        }
        /// <summary>
        /// SM4国标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xtraTabControHome.Name = "Sm4WinForm";
            Sm4WinForm Sm4WinForm = new Sm4WinForm();
            LoadTabPageForm(xtraTabControl, Sm4WinForm, "SM4国标");
        }
        /// <summary>
        /// base64互转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBase64_Click(object sender, EventArgs e)
        {
            xtraTabControHome.Name = "Base64WinForm";
            Base64WinForm Base64WinForm = new Base64WinForm();
            LoadTabPageForm(xtraTabControl, Base64WinForm, "Base64互转");
        }
        /// <summary>
        /// Base64互转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xtraTabControHome.Name = "Base64WinForm";
            Base64WinForm Base64WinForm = new Base64WinForm();
            LoadTabPageForm(xtraTabControl, Base64WinForm, "Base64互转");
        }
        /// <summary>
        /// Url编码解码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUrl_Click(object sender, EventArgs e)
        {
            xtraTabControHome.Name = "UrlWinForm";
            UrlWinForm Base64WinForm = new UrlWinForm();
            LoadTabPageForm(xtraTabControl, Base64WinForm, "URL编码");
        }
        /// <summary>
        /// Url编码解码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem6_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xtraTabControHome.Name = "UrlWinForm";
            UrlWinForm Base64WinForm = new UrlWinForm();
            LoadTabPageForm(xtraTabControl, Base64WinForm, "URL编码");
        }
        /// <summary>
        /// XML格式化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnXml_Click(object sender, EventArgs e)
        {
            xtraTabControHome.Name = "XMLWinForm";
            XMLWinForm XMLWinForm = new XMLWinForm();
            LoadTabPageForm(xtraTabControl, XMLWinForm, "XML格式化");
        }
        /// <summary>
        /// XML格式化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xtraTabControHome.Name = "XMLWinForm";
            XMLWinForm XMLWinForm = new XMLWinForm();
            LoadTabPageForm(xtraTabControl, XMLWinForm, "XML格式化");
        }
        /// <summary>
        /// Json格式化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnJson_Click(object sender, EventArgs e)
        {
            xtraTabControHome.Name = "JsonWinForm";
            JsonWinForm JsonWinForm = new JsonWinForm();
            LoadTabPageForm(xtraTabControl, JsonWinForm, "JSON格式化");
        }
        /// <summary>
        /// Json格式化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xtraTabControHome.Name = "JsonWinForm";
            JsonWinForm JsonWinForm = new JsonWinForm();
            LoadTabPageForm(xtraTabControl, JsonWinForm, "JSON格式化");
        }
        /// <summary>
        /// 二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnScanCode_Click(object sender, EventArgs e)
        {
            xtraTabControHome.Name = "ScanCodeWinForm";
            ScanCodeWinForm ScanCodeWinForm = new ScanCodeWinForm();
            LoadTabPageForm(xtraTabControl, ScanCodeWinForm, "二维码/条形码");
        }
        /// <summary>
        /// 二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xtraTabControHome.Name = "ScanCodeWinForm";
            ScanCodeWinForm ScanCodeWinForm = new ScanCodeWinForm();
            LoadTabPageForm(xtraTabControl, ScanCodeWinForm, "二维码/条形码");
        }
        /// <summary>
        /// Request请求
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRequest_Click(object sender, EventArgs e)
        {
            xtraTabControHome.Name = "RequestWinForm";
            RequestWinForm RequestWinForm = new RequestWinForm();
            LoadTabPageForm(xtraTabControl, RequestWinForm, "Request请求");
        }
        /// <summary>
        /// Request请求
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xtraTabControHome.Name = "RequestWinForm";
            RequestWinForm RequestWinForm = new RequestWinForm();
            LoadTabPageForm(xtraTabControl, RequestWinForm, "Request请求");
        }
        /// <summary>
        /// 电脑信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPc_Click(object sender, EventArgs e)
        {
            //等待窗体提示
            SplashScreen2 splashScreen2 = new SplashScreen2();
            splashScreen2.ShowMessage();
            xtraTabControHome.Name = "HardwareInfoForm";
            HardwareInfoForm HardwareInfoForm = new HardwareInfoForm();
            LoadTabPageForm(xtraTabControl, HardwareInfoForm, "电脑信息");
            splashScreen2.HideMessage();
        }
        /// <summary>
        /// 格式化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnWordCode_Click(object sender, EventArgs e)
        {
            MessageBox.Show("开发中,请期待......");
        }
        /// <summary>
        /// Redis
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRedis_Click(object sender, EventArgs e)
        {
            xtraTabControHome.Name = "RedisWinForm";
            RedisWinForm RedisWinForm = new RedisWinForm();
            LoadTabPageForm(xtraTabControl, RedisWinForm, "Redis");
        }
        /// <summary>
        /// RabbitMQ
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRabbitMQ_Click(object sender, EventArgs e)
        {
            xtraTabControHome.Name = "RabbitMQWinForm";
            RabbitMQWinForm RabbitMQWinForm = new RabbitMQWinForm();
            LoadTabPageForm(xtraTabControl, RabbitMQWinForm, "RabbitMQ");
        }

        private void xtraTabControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                xtraTabControl.ContextMenuStrip = null;
                xtraTabControl.ContextMenuStrip = ContextMenuStrip;

            }
        }
        /// <summary>
        /// 双击关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void xtraTabControl_DoubleClick(object sender, EventArgs e)
        {
            XtraTabHitInfo tabHitInfo = ((XtraTabControl)sender).CalcHitInfo(((XtraTabControl)sender).PointToClient(Control.MousePosition));
            ((XtraTabControl)sender).TabPages.Remove(tabHitInfo.Page);
        }

        private void contextMenuStrip1_MouseDown(object sender, MouseEventArgs e)
        {

        }
        /// <summary>
        /// 工具箱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem10_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            xtraTabControHome.Name = "ToolsDateTimeWinForm";
            ToolsDateTimeWinForm ToolsDateTimeWinForm = new ToolsDateTimeWinForm();
            LoadTabPageForm(xtraTabControl, ToolsDateTimeWinForm, "工具箱");
        }
        /// <summary>
        /// 工具箱
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnToolDate_Click(object sender, EventArgs e)
        {
            xtraTabControHome.Name = "ToolsDateTimeWinForm";
            ToolsDateTimeWinForm ToolsDateTimeWinForm = new ToolsDateTimeWinForm();
            LoadTabPageForm(xtraTabControl, ToolsDateTimeWinForm, "工具箱");
        }
        /// <summary>
        /// 添加窗体最小化事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenusHome_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.notifyIcon1.Visible = true;
            }
        }

        /// <summary>
        /// 双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                //还原窗体显示    
                WindowState = FormWindowState.Normal;
                //激活窗体并给予它焦点
                this.Activate();
                //任务栏区显示图标
                this.ShowInTaskbar = true;
                //托盘区图标隐藏
                //  notifyIcon1.Visible = false;
            }
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();                                //窗体显示
            this.WindowState = FormWindowState.Normal;  //窗体状态默认大小
            this.Activate();
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //点击"是(YES)"退出程序
            if (MessageBox.Show("确定要退出程序?", "安全提示",
                        System.Windows.Forms.MessageBoxButtons.YesNo,
                        System.Windows.Forms.MessageBoxIcon.Warning)
                == System.Windows.Forms.DialogResult.Yes)
            {
                notifyIcon1.Visible = false;   //设置图标不可见
                this.Close();                  //关闭窗体
                this.Dispose();                //释放资源
                Application.Exit();            //关闭应用程序窗体
            }
        }
        /// <summary>
        /// 点击鼠标"左键"发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            //点击鼠标"左键"发生
            if (e.Button == MouseButtons.Left)
            {
                this.Visible = true;                        //窗体可见
                this.WindowState = FormWindowState.Normal;  //窗体默认大小
                this.notifyIcon1.Visible = true;            //设置图标可见
            }
        }
        /// <summary>
        /// /窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenusHome_FormClosing(object sender, FormClosingEventArgs e)
        {
            //窗体关闭原因为单击"关闭"按钮或Alt+F4
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;           //取消关闭操作 表现为不关闭窗体
                this.Hide();               //隐藏窗体
            }
        }
    }
}
